package com.zhongyouyun.wx.wxapplet.jyj.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.jyj.model.DistrictModel;
import com.zhongyouyun.jyj.model.SspNoticeModel;
import com.zhongyouyun.jyj.model.ReportModel;
import com.zhongyouyun.jyj.service.DistrictService;
import com.zhongyouyun.jyj.service.SspNoticeService;
import com.zhongyouyun.jyj.service.ReportService;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.wx.wxapplet.jyj.pojo.ReportPojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("wx/wxapplet/jyj/wxApp")
@Api(value = "教育局-微信小程序功能接口", tags = {"教育局-微信小程序功能接口"})
public class WxAppletSspController {

    @Autowired
    private SspNoticeService noticeService;
    @Autowired
    private ReportService reportService;
    @Autowired
    private DistrictService districtService;

    @GetMapping("/getDistrictList")
    @ResponseBody
    @ApiOperation(value = "微信小程序获取地区", notes = "微信小程序获取地区")
    public Result getDistrictList() {
        List<DistrictModel> districtModels = districtService.selectList(new QueryWrapper<>());
        return Result.builder().message("成功").status(0).data(districtModels).build();
    }

    @GetMapping("/getNotic")
    @ResponseBody
    @ApiOperation(value = "微信小程序获取公告", notes = "微信小程序获取公告")
    public Result getCode() {
        SspNoticeModel noticeModel = noticeService.getOne(new QueryWrapper<>());
        return Result.builder().message("成功").status(0).data(noticeModel).build();
    }

    @PostMapping("/saveReport")
    @ResponseBody
    @ApiOperation(value = "微信小程序投诉内容保存", notes = "微信小程序投诉内容保存")
    public Result saveReport(@RequestBody ReportPojo reportPojo) {
        if (ObjectUtil.isNotEmpty(reportPojo)) {
            ReportModel reportModel = new ReportModel();
            reportModel.setOpenId(reportPojo.getOpenId());
            reportModel.setName(reportPojo.getName());
            reportModel.setPosition(reportPojo.getPosition());
            reportModel.setDistrict(reportPojo.getDistrict());
            reportModel.setState("0");
            boolean flag = reportService.saveOrUpdate(reportModel, reportPojo.getUrls());
            if (flag) {
                return Result.builder().message("保存成功").status(0).build();
            } else {
                return Result.builder().message("保存失败").success(false).status(1).build();
            }
        } else {
            return Result.builder().message("保存失败").success(false).status(1).build();
        }
    }
}
